package com.refactor.practice;

import com.refactor.dto.ResultDto;
import com.refactor.enums.MovieTypes;
import com.refactor.impl.PointHandler;

import java.util.Enumeration;
import java.util.Optional;
import java.util.Vector;

public class Customer {
	private String _name;
	private Vector _rentals;

	public Customer(String _name, Vector _rentals) {
		this._name = _name;
		this._rentals = _rentals;
	}

	public String getName() {
		return _name;
	}

	public void addRental(Rental arg) {
		_rentals.add(arg);
	}

	public String statement(){
		double totalAmount = 0;
		int frequentRenterPoints = 0;
		Enumeration rentals = _rentals.elements();
		String result = "Rental Record for " + getName() + "\n";
		while (rentals.hasMoreElements()) {
			double thisAmount = 0D;
			Rental each = (Rental) rentals.nextElement();
			MovieTypes sourceTypeEnum = null;
			try {
				sourceTypeEnum = Optional.ofNullable(MovieTypes.parse(each.get_type()))
						.orElseThrow(() -> new Exception("type error"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			PointHandler instance = sourceTypeEnum.instance(each.get_type());
			ResultDto hanler = instance.hanler(each);
			totalAmount += hanler.getAmount();
			result += hanler.getResult();
			frequentRenterPoints+=hanler.getPoint();
		}
		//add footer lines
		result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
		result += "You earned " + String.valueOf(frequentRenterPoints) +
				" frequent renter points";

		System.out.println("result:");
		System.out.println(result);
		return result;
	}
}
